from django.db import models
from django.contrib.auth.models import AbstractUser
from utils.models import BaseModel
from areas.models import Area
# Create your models here.

class User(AbstractUser):
    mobile = models.CharField(verbose_name='手机号',max_length=11,unique=True)
    email_active = models.BooleanField(verbose_name='邮箱激活状态',default=False)
    default_address = models.ForeignKey('Address',on_delete=models.SET_NULL,related_name='users',null=True,blank=True,verbose_name='默认地址')
    class Meta:
        db_table = 'tb_users'
        verbose_name_plural = '用户'


class Address(BaseModel):
    title = models.CharField(max_length=20,verbose_name='地址标题')
    receiver = models.CharField(max_length=20,verbose_name='收件人')
    mobile = models.CharField(max_length=11,verbose_name='手机号')
    province = models.ForeignKey(Area,on_delete=models.PROTECT,related_name='province_addresses',verbose_name='省')
    city = models.ForeignKey(Area,on_delete=models.PROTECT,related_name='city_addresses',verbose_name='市')
    district = models.ForeignKey(Area,on_delete=models.PROTECT,related_name='district_addresses',verbose_name='县')
    place = models.CharField(max_length=50,verbose_name='详细地址')
    email = models.CharField(max_length=30,null=True,blank=True,default='',verbose_name='邮件地址')
    tel = models.CharField(max_length=15,null=True,blank=True,default='',verbose_name='固定电话')
    user = models.ForeignKey(User,on_delete=models.CASCADE,related_name='addresses',verbose_name='所属用户')
    is_deleted = models.BooleanField(default=False,verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_addresses'
        verbose_name_plural = '收货地址表'
        ordering = ['-update_time']
